<?php

class comment extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('comment_model');
        $this->load->library('pagination');
        $this->load->helper('url');
        //$this->load->helper('form');
    }

    public function index()
    {
        $data['title'] = "List comment";
        $data['path'] = "comment/list";

        $config['base_url'] = base_url().'admin/comment/index';
        $config['total_rows'] = $this->comment_model->totalComment();
        $config['uri_segment'] = 4;
        $config['per_page'] = 2; // Number record show per page
        $this->pagination->initialize($config);
        $st = $this->uri->segment(4);

        $data['comment'] = $this->comment_model->listComment($config['per_page'], $st);
        $data['nameproduct'] = $this->comment_model->nameProduct();
        $this->load->view("index", $data);
    }

    public function del()
    {
        $id = $this->uri->segment(4);
        $this->comment_model->delete($id);
        redirect(base_url() . "index.php/admin/comment");
    }

    public function checkShow()
    {
        $data['title'] = "List comment";
        $data['path'] = "comment/list";
        $config['base_url'] = base_url() . "index.php/admin/comment/index";
        $config['total_rows'] = $this->comment_model->totalComment();
        $config['uri_segment'] = 4;
        $config['per_page'] = 5; // Number record show per page
        $this->pagination->initialize($config);

        if ($this->input->post('show') != NULL) {
            if (isset($_POST['check'])) {

                foreach ($_POST['check'] as $key => $value) {

                    $this->comment_model->updateShow($value);
                }
                redirect(base_url() . "index.php/admin/comment");

            } else {
                $data['comment'] = $this->comment_model->listComment($config['per_page'], $this->uri->segment(4));
                $data['nameproduct'] = $this->comment_model->nameProduct();
                $this->load->view("index", $data);
            }
        } elseif ($this->input->post('delete') != NULL) {
            if (isset($_POST['check'])) {
                foreach ($_POST['check'] as $key => $value) {
                    $this->comment_model->delete($value);
                }
                redirect(base_url() . "index.php/admin/comment");
            } else {
                $data['comment'] = $this->comment_model->listComment($config['per_page'], $this->uri->segment(4));
                $data['nameproduct'] = $this->comment_model->nameProduct();
                $this->load->view("index", $data);
            }
        } else {
            $data['comment'] = $this->comment_model->listComment($config['per_page'], $this->uri->segment(4));
            $data['nameproduct'] = $this->comment_model->nameProduct();
            $this->load->view("index", $data);
        }
    }

    public function detail()
    {
        $id = $this->uri->segment(4);
        $data['title'] = "Detail title";
        $data['path'] = "comment/detail";
        $data['info'] = $this->comment_model->getdata($id);
        $data['nameproduct'] = $this->comment_model->nameProduct();
        $this->load->view("index",$data);

    }
}